package cn.tedu.jdbc;

import cn.tedu.Main;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * @Author: Miyako
 * @Date: 2024-07-30-15:18
 * @Description: 测试封装的connection连接工具
 */
public class JDBCDemo04 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
//        System.out.println("输入你要连接的数据库名");
//        String dB = scanner.nextLine();
        Connection c = null;
        try {
                c = DBUtilPool.getConnection();

            Statement statement = c.createStatement();
            //编写sql语句
            while (true) {
                System.out.println("输入用户名");
                String username = scanner.nextLine();
                System.out.println("输入密码");
                String password = scanner.nextLine();
                System.out.println("输入昵称");
                String nickname = scanner.nextLine();
                System.out.println("输入年龄");
                int age = scanner.nextInt();

                String sql = "INSERT INTO userinfo(username,password,nickname,age) " +
                        "VALUES(" + "'" + username + "','" + password + "','" + nickname + "','" + age + "')";

//        String sql = "INSERT INTO userinfo(username,password,nickname,age) VALUES('Tony','123456','tony','65')";
//        String sql = "delete from userinfo where username='Tony'";
                //执行sql语句，然后返回一个整型其值是受影响的数据条数

                int j = statement.executeUpdate(sql);
                if (j == 0) {
                    System.out.println("添加失败,exit退出");
                } else {
                    System.out.println("添加成功捏，exit退出");
                }

                if ("exit".equals(scanner.nextLine())) {
                    break;
                }
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            scanner.close();
            try{//断开数据库连接
                if (c!=null)
                c.close();
            }catch (SQLException e){
            }
        }

    }
}
